//
//  DSSearchViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2020/12/11.
//  Copyright © 2020 zhangbenchao. All rights reserved.
//


#import "DSSearchViewController.h"
#import "BkSearchResultViewController.h"
#import "JZSearchItemsView.h"


#import "LXSaveUserInforTool.h"

@interface DSSearchViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *searchTextFiled;
@property (nonatomic,strong) UIImageView *searchImg;
@property (nonatomic,strong) JZSearchItemsView *historyView;

@property (nonatomic,strong) NSMutableArray *historyArray;
@property (nonatomic,strong) JZSearchItemsView *hotView;
@end

@implementation DSSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self.view addSubview:self.searchTextFiled];
    [self.view addSubview:self.historyView];
    self.navigationItem.titleView = self.searchTextFiled;
    [self addRightNavItemWithTitle:@"搜索" color:kMainTxtColor font:systemFont(TSScaleW(14))];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self  historyDataConfig];
}
-(void)rigthBtnAction:(id)sender{
    BkSearchResultViewController *vc = [[BkSearchResultViewController alloc]init];
    vc.searchWords = _searchTextFiled.text;
    [self.navigationController pushViewController:vc animated:YES];
    !self.callBackBlock?:self.callBackBlock(_searchTextFiled.text);
}
-(void)historyDataConfig
{
    NSString *docuPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *string = [NSString stringWithFormat:@"/historySearch.txt"];
    docuPath =[docuPath stringByAppendingString:string];
    //写入文件
    
    //取出文件
    NSArray *array = [NSArray arrayWithContentsOfFile:docuPath];
    if (!array) {
        array = @[];
    }
    self.historyArray = [NSMutableArray arrayWithArray:array];
    self.historyView.dataArray = self.historyArray;
    [array writeToFile:docuPath atomically:YES];
    NSLog(@"docuPath = %@",docuPath);
   // [self requstHotWords];
    [self.view addSubview:self.hotView];
}



-(UITextField *)searchTextFiled{
    if (!_searchTextFiled) {
        _searchTextFiled = [TSWLTools allocTextFieldTextFont:TSScaleW(14) placeHolderFont:TSScaleW(14) text:nil placeText:@"输入关键词搜索" textColor:kMainTxtColor placeHolderTextColor:kGrayTxtColor frame:CGRectMake(TSScaleW(12), TSScaleW(10),TSScaleW(210), TSScaleW(34))];
        _searchTextFiled.cornerRadius = _searchTextFiled.height/2.f;
        _searchTextFiled.leftViewMode = UITextFieldViewModeAlways;
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, TSScaleW(34), _searchTextFiled.height)];
        [view addSubview:self.searchImg];
        _searchTextFiled.leftView = view;
        _searchTextFiled.backgroundColor = kMainBgColor;
        _searchTextFiled.delegate = self;
    }
    return _searchTextFiled;
}
-(UIImageView *)searchImg{
    if (!_searchImg) {
        _searchImg = [[UIImageView alloc ]initWithFrame:CGRectMake(TSScaleW(15), TSScaleW(9), TSScaleW(12), TSScaleW(12))];
        _searchImg.image = [[UIImage imageNamed:@"搜索"] imageChangeColor:kMainTxtColor];
        _searchImg.centerY = _searchTextFiled.height/2.f;
    }
    return _searchImg;
}
-(JZSearchItemsView *)historyView{
    if (!_historyView) {
        _historyView = [[JZSearchItemsView alloc]initWithTop:_searchTextFiled.bottom + TSScaleW(0) dataArray:@[@"保姆",@"小保姆",@"123456保姆",@"钟点工",@"月嫂"] isDelete:YES];
        WS(weakSelf);
        _historyView.titleLabel.text = @"历史搜索";
        [LXSaveUserInforTool sharedUserTool].type = 2;
        
        _historyView.selectBlock = ^(NSString * _Nonnull carLong) {
           
            !weakSelf.callBackBlock?:weakSelf.callBackBlock(carLong);
            BkSearchResultViewController *vc = [[BkSearchResultViewController alloc]init];
            vc.searchWords = carLong;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            //[weakSelf.navigationController popViewControllerAnimated:YES];
        };
        _historyView.callFrameBack = ^{
        
        };
        _historyView.deleteBlock = ^{
            [weakSelf.historyArray removeAllObjects];
            NSString *docuPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
            docuPath =[docuPath stringByAppendingString:@"/historySearch.txt"];
            [weakSelf.historyArray writeToFile:docuPath atomically:YES];
            
        };
    }
    return _historyView;
}

//-(JZSearchItemsView *)hotView{
//    if (!_hotView) {
//        _hotView = [[JZSearchItemsView alloc]initWithTop:_historyView.bottom + TSScaleW(25) dataArray:@[@"123456保姆",@"钟点工",@"月嫂"] isDelete:NO];
//        WS(weakSelf);
//        _hotView.titleLabel.text = @"热门搜索";
//              _hotView.selectBlock = ^(NSString * _Nonnull carLong) {
//                  DSSearchResultViewController *vc = [[DSSearchResultViewController alloc]init];
//                  //vc.content = textField.text;
//                  vc.searchWords = carLong;
//                  [weakSelf naviPushVc:vc];
//              };
//    }
//    return _hotView;
//}
//
//-(void)requstHotWords{
//
//    NSDictionary * pamas = @{};
//       [NetWorkTools postConJSONWithUrl:@"hotKeywords" parameters:pamas success:^(id responseObject) {
//           NSString *result = responseObject[@"result"];
//           NSString *resultNote = responseObject[@"resultNote"];
//           NSArray *keys = responseObject[@"keys"];
//
//           if (result.integerValue == 0) {
//
//
//             //  [self handleListWithModel:muArray];
//               self.hotView.dataArray = keys;
//           }else{
//                [MBProgressHUD showError:resultNote];
//           }
//
//       } fail:^(NSError *error) {
//
//       }];
//}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (![self.historyArray containsObject:textField.text]&&textField.text.length) {
        [self.historyArray insertObject:textField.text atIndex:0];
        if (self.historyArray.count > 6) {
            for (int i = 0; i < self.historyArray.count; i++) {
                if (i>=6) {
                    [self.historyArray removeObject:[self.historyArray objectAtIndex:i]];
                }
            }
        }
    }
    NSString *docuPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *string = [NSString stringWithFormat:@"/historySearch.txt"];
    docuPath =[docuPath stringByAppendingString:string];
    [self.historyArray writeToFile:docuPath atomically:YES];
    NSLog(@"docuPath = %@",docuPath);
    if (textField.text.length) {
//        BkAgeListViewController *vc = [[BkAgeListViewController alloc]init];
//        vc.searchWords = textField.text;
//        [self.navigationController pushViewController:vc animated:YES];
//        !self.callBackBlock?:self.callBackBlock(textField.text);
        //[self.navigationController popViewControllerAnimated:YES];
       
    }
}

-(NSMutableArray *)historyArray{
    if (!_historyArray) {
        _historyArray = [NSMutableArray array];
    }
    return _historyArray;
}
@end
